package com.snts.synchronization.collector;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.UUID;

import javax.sql.DataSource;

import org.apache.commons.collections.MultiHashMap;
import org.apache.commons.collections.MultiMap;

import com.snts.synchronization.core.Change;
import com.snts.synchronization.server.TransactionSource;
import com.snts.synchronization.transaction.Transaction;

public class DefaultCollector implements Collector, TransactionSource {

	private UpdateQueue updateQueue = new UpdateQueueImpl();

	// Dependencies
	private DataSource dataSource;
	// Dependencies
	
	@Override
	public void addChanges(List<Change> changes) {
		try {
			Connection conn = dataSource.getConnection();
			try {

			} finally {
				conn.close();
			}			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.snts.synchronization.server.TransactionSource#getTransactions(java.util.UUID)
	 */
	
	public DataSource getDataSource() {
		return dataSource;
	}

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	@Override
	public List<Transaction> getTransactions(UUID restaurantId) {
		List<Transaction> transactions = new ArrayList<Transaction>();
		Transaction transaction = null;
		while((transaction = updateQueue.poll(restaurantId)) != null){
			transactions.add(transaction);
		}
		Collections.sort(transactions, new Comparator<Transaction>() {
			@Override
			public int compare(Transaction o1, Transaction o2) {
				return (int) (o1.getEndTime().getTime() - o2.getEndTime().getTime());
			}
		});
		return transactions;
	}
	
	
}
